<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script type="module">
        import { pajax } from "./util.js"

        //1.函数外面捕获错误
        // async function test() {
        //     //await 同步代码/promise对象
        //     const res = await pajax({
        //         url: "http://localhost:3000/news",
        //         data: {
        //             author: "kerwin"
        //         }
        //     })
        //     console.log(res);//只影响函数里面
        //     // console.log(666);

        //     const res1 = await pajax({
        //         url: "http://localhost:3000/comments",
        //         data: {
        //             newsId: res[0].id
        //         }
        //     })
        //     console.log(res1);
        //     return res1;//返回的也是Promise对象
        // }

        // test().then(res => {
        //     console.log("返回结果", res);
        // }).catch(err => {
        //     console.log("err", err);
        // })

        // console.log("11111");


        //2.函数里面捕获错误
        async function test1() {
            //await 同步代码/promise对象
            try {
                const res = await pajax({
                    url: "http://localhost:3000/news",
                    data: {
                        author: "kerwin"
                    }
                })
                console.log(res);//只影响函数里面

                const res1 = await pajax({
                    url: "http://localhost:3000/comments",
                    data: {
                        newsId: res[0].id
                    }
                })
                console.log(res1);
                console.log(typeof res1 === "object");
            } catch (error) {
                console.log("error", error);
            }
        }

        console.log(test1());//返回的是一个Promise对象

    </script>
</body>

</html>